<?php
require_once BACKGROUND_COMMON_ACTION_SERVICE_DIR . '/default.php';

class background_good_add_action_service extends background_default_common_action_service
{
    protected static $_instance;

    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


    protected function _current_page_process()
    {
        $result = array('status' => 0, 'message' => '', 'data' => array());
	    
	    $check_save_data_result = $this->check_save_data();
	    if($check_save_data_result['status'] < 0){    	        
	        $result['status'] = $check_save_data_result['status'];
	        $result['message'] = $check_save_data_result['message'];
	    }else{
	        $good_info = $check_save_data_result['data']['good_info'];
	    	    
    	    $write_db = $this->_get_write_db();
    	    $pdo = $write_db->get_connection();
    	    $pdo->beginTransaction();
    	    
    	    try{	        	        
        	    $model_background_good = wwy_resource_container::get_model_background_good();
        	    
        	    $create_time = time();
        	    $good_info['click'] = !empty($good_info['click']) ? $good_info['click'] : 0;
        	    $good_info['create_time'] = !empty($good_info['create_time']) ? $good_info['create_time'] : $create_time;
        	    $good_info['update_time'] = !empty($good_info['update_time']) ? $good_info['update_time'] : $create_time;
        	    $add_result = $model_background_good->add($good_info);
        	    //var_dump($add_result); exit();
        	    $good_id = $add_result;
        	    $good_info['id'] = $good_id;    	    
        	    
        	    $good_attr_list = $good_info['attr_list'];
        	    $model_background_good->add_good_attr_list($good_info, $good_attr_list);
        	    
        	    $good_sattr_list = $good_info['sattr_list'];
        	    $good_sattr_list = $model_background_good->add_good_sattr_list($good_info, $good_sattr_list);
        	            	    
        	    $good_number_grade_list = $good_info['number_grade_list'];
        	    $good_number_grade_list = $model_background_good->add_good_number_grade_list($good_info, $good_number_grade_list);
        	    
        	    $good_spec_list = $good_info['spec_list'];
        	    $model_background_good->add_good_spec_list($good_info, $good_spec_list, $good_sattr_list, $good_number_grade_list);    	     
        	        	    
        	    $result['status']  = 1;
        	    $result['message'] = '新增商品成功';
        	    self::add_view_data('good_id', $good_id);
        	    
        	    wwy_resource_container::get_cache_handler()->flush();
    	    
    			$pdo->commit();
    		}catch (Exception $e){
    			$pdo->rollBack();
    			
    			throw $e;
    		}
	    }
		
		self::set_view_status($result['status']);
		self::set_view_message($result['message']);
    }
    
    /****************** @start private function  ******************/
    public function check_save_data()
    {
        $result = array('status' => 0, 'message' => '', 'data' => array());
         
        $user_data = $_POST;
        $id               = init_user_data_key($_POST, 'id', 'int');    //更新时才有【$id】
        $cat_id           = init_user_data_key($user_data, 'cat_id', 'int');
        $name             = init_user_data_key($user_data, 'name', 'str');
        $brief            = init_user_data_key($user_data, 'brief', 'str');
        $description      = init_user_data_key($user_data, 'description', 'str');
        $after_sale_service = init_user_data_key($user_data, 'after_sale_service', 'str');
        $order            = init_user_data_key($user_data, 'order', 'int');
        $status           = init_user_data_key($user_data, 'status', 'int');
    
        //数组：图片
        $image_arr = isset($_POST['image_arr']) ? $_POST['image_arr'] : null;
        //var_dump($image_arr); exit();
         
        //数组：属性规格
        $attr_list  = isset($_POST['attr_list']) ? $_POST['attr_list'] : null;
        $sattr_list = isset($_POST['sattr_list']) ? $_POST['sattr_list'] : null;
        $number_grade_list = isset($_POST['number_grade_list']) ? $_POST['number_grade_list'] : null;
        $spec_list  = isset($_POST['spec_list']) ? $_POST['spec_list'] : null;
    
        if(empty($cat_id)){
            $result['status']  = -1;
            $result['message'] = '请选择分类';
    
            return $result;
        }
        if(empty($name)){    //输入【0】也会报错，除非加【 && ($name !== '0')】
            $result['status']  = -2;
            $result['message'] = '请输入商品名称';
    
            return $result;
        }
        if(empty($brief)){
            $result['status']  = -3;
            $result['message'] = '请输入简述';
    
            return $result;
        }
        if(empty($description)){
            $result['status']  = -4;
            $result['message'] = '请输入描述';
    
            return $result;
        }
        if(empty($after_sale_service)){
            $result['status']  = -4;
            $result['message'] = '请输入售后服务';
             
            return $result;
        }
        if(empty($image_arr)){
            $result['status']  = -5;
            $result['message'] = '请上传图片';
    
            return $result;
        }
        if((empty($order) && $order !== 0) || $order < 0){
            $result['status']  = -10;
            $result['message'] = '请输入排序';
    
            return $result;
        }
        if((empty($status) && $status !== 0) || $status < 0){
            $result['status']  = -11;
            $result['message'] = '请选择状态';
    
            return $result;
        }
        //exit();
         
        $check_attr_list_result = $this->check_attr_list($attr_list);
        if($check_attr_list_result['status'] < 0){
            return $check_attr_list_result;
        }else{
            $attr_list = $check_attr_list_result['data']['attr_list'];
        }
         
        $check_sattr_list_result = $this->check_sattr_list($sattr_list);
        if($check_sattr_list_result['status'] < 0){
            return $check_sattr_list_result;
        }else{
            $sattr_list = $check_sattr_list_result['data']['sattr_list'];
        }
         
        $check_number_grade_list_result = $this->check_number_grade_list($number_grade_list);
        if($check_number_grade_list_result['status'] < 0){
            return $check_number_grade_list_result;
        }else{
            $number_grade_list = $check_number_grade_list_result['data']['number_grade_list'];
        }
         
         
        $sattr_count = count($sattr_list);
        $number_grade_count = count($number_grade_list);
        $check_spec_list_result = $this->check_spec_list($spec_list, $sattr_count, $number_grade_count);
        if($check_spec_list_result['status'] < 0){
            return $check_spec_list_result;
        }else{
            $spec_list = $check_spec_list_result['data']['spec_list'];
        }
         
        $model_background_good_category = wwy_resource_container::get_model_background_good_category();
        $good_category_info = $model_background_good_category->find($cat_id);
        if(empty($good_category_info)){
            $result['status']  = -400;
            $result['message'] = '该分类不存在';
    
            return $result;
        }
         
        //更新时才有【$id】
        if(!empty($id)){
            $model_background_good = wwy_resource_container::get_model_background_good();
             
            $good_info = $model_background_good->find($id);
            if(empty($good_info)){
                $result['status']  = -400;
                $result['message'] = '该商品不存在';
                 
                return $result;
            }
        }
         
        $good_info = array(
            'id' => $id,
            'cat_id' => $cat_id,
            'name' => $name,
            'brief' => $brief,
            'description' => $description,
            'after_sale_service' => $after_sale_service,
            'image_arr' => $image_arr,
            'order' => $order,
            'status' => $status,
            'if_show_number_grade' => $number_grade_count > 1 ? 1 : 0,
             
            'attr_list' => $attr_list,
            'sattr_list' => $sattr_list,
            'number_grade_list' => $number_grade_list,
            'spec_list' => $spec_list,
        );
        $result['data']['good_info'] = $good_info;
         
        return $result;
    }
    
    public function check_attr_list($attr_list)
    {
        $result = array('status' => 0, 'message' => '', 'data' => array());
         
        if(empty($attr_list)){
            $result['status']  = -12;
            $result['message'] = '请输入属性列表';
    
            return $result;
        }
         
        if(!is_array($attr_list)){
            $result['status']  = -13;
            $result['message'] = '属性列表格式错误';
             
            return $result;
        }
         
        $no = 1;
        foreach($attr_list as $key => $attr){
            $no_desc = '第' . $no . '个属性：';
             
            $name  = init_user_data_key($attr, 'name', 'str');
            $value = init_user_data_key($attr, 'value', 'str');
            $order = init_user_data_key($attr, 'order', 'int');
            if(empty($name)){
                $result['status']  = -400;
                $result['message'] = $no_desc . '请输入名称';
                 
                return $result;
            }
            if($value === ''){
                $result['status']  = -400;
                $result['message'] = $no_desc . '请输入值';
                 
                return $result;
            }
            if((empty($order) && $order !== 0) || $order < 0){
                $result['status']  = -400;
                $result['message'] = $no_desc . '请输入正确的排序';
                 
                return $result;
            }
             
            $attr_list[$key]['name'] = $name;
            $attr_list[$key]['value'] = $value;
            $attr_list[$key]['order'] = $order;
             
            $no++;
        }
         
        $result['data']['attr_list'] = $attr_list;
         
        return $result;
    }
    
    public function check_sattr_list($sattr_list)
    {
        $result = array('status' => 0, 'message' => '', 'data' => array());
    
        if(empty($sattr_list)){
            $result['status']  = -400;
            $result['message'] = '请输入规格属性列表';
             
            return $result;
        }
    
        if(!is_array($sattr_list)){
            $result['status']  = -400;
            $result['message'] = '规格属性列表格式错误';
    
            return $result;
        }
    
        $no = 1;
        foreach($sattr_list as $key => $sattr){
            $no_desc = '第' . $no . '个规格属性：';
    
            $name  = init_user_data_key($sattr, 'name', 'str');
            $order = init_user_data_key($sattr, 'order', 'int');
            if(empty($name)){
                $result['status']  = -400;
                $result['message'] = $no_desc . '请输入名称';
    
                return $result;
            }
            if((empty($order) && $order !== 0) || $order < 0){
                $result['status']  = -400;
                $result['message'] = $no_desc . '请输入正确的排序';
    
                return $result;
            }
             
            $sattr_list[$key]['name'] = $name;
            $sattr_list[$key]['order'] = $order;
    
            $no++;
        }
         
        $result['data']['sattr_list'] = $sattr_list;
    
        return $result;
    }
    
    public function check_number_grade_list($number_grade_list)
    {
        $result = array('status' => 0, 'message' => '', 'data' => array());
    
        if(empty($number_grade_list)){
            $result['status']  = -400;
            $result['message'] = '请输入数量范围列表';
    
            return $result;
        }
    
        if(!is_array($number_grade_list)){
            $result['status']  = -400;
            $result['message'] = '数量范围列表格式错误';
    
            return $result;
        }
    
        $no = 1;
        foreach($number_grade_list as $key => $number_grade){
            $no_desc = '第' . $no . '个数量范围：';
    
            $number_min  = init_user_data_key($number_grade, 'number_min', 'int');
            $number_max  = init_user_data_key($number_grade, 'number_max', 'int');
            $order = init_user_data_key($number_grade, 'order', 'int');
            if((empty($number_min) && $number_min !== 0) || $number_min < 0 || $number_min > 999999){
                $result['status']  = -400;
                $result['message'] = $no_desc . '请输入正确的起订数量，最大为999999';
                 
                return $result;
            }
            if((empty($number_max) && $number_max !== 0) || $number_max < 0 || $number_max > 999999){
                $result['status']  = -400;
                $result['message'] = $no_desc . '请输入正确的止订数量，最大为999999';
                 
                return $result;
            }
            if( ($number_max !== 0) && ($number_max < $number_min) ){
                $result['status']  = -400;
                $result['message'] = $no_desc . '止订数量不能小于起订数量';
    
                return $result;
            }
             
            if((empty($order) && $order !== 0) || $order < 0){
                $result['status']  = -400;
                $result['message'] = $no_desc . '请输入正确的排序';
    
                return $result;
            }
    
            $number_grade_list[$key]['number_min'] = $number_min;
            $number_grade_list[$key]['number_max'] = $number_max;
            $number_grade_list[$key]['order'] = $order;
    
            $no++;
        }
    
        $result['data']['number_grade_list'] = $number_grade_list;
    
        return $result;
    }
    
    public function check_spec_list($spec_list, $sattr_count, $number_grade_count)
    {
        $result = array('status' => 0, 'message' => '', 'data' => array());
         
        if(empty($spec_list)){
            $result['status']  = -400;
            $result['message'] = '请输入规格列表';
             
            return $result;
        }
    
        if(!is_array($spec_list)){
            $result['status']  = -400;
            $result['message'] = '规格列表格式错误';
    
            return $result;
        }
         
        $no = 1;
        foreach ($spec_list as $key => $spec) {
            $no_desc = '第' . $no . '个规格：';
    
            $order = init_user_data_key($spec, 'order', 'int');
            if((empty($order) && $order !== 0) || $order < 0){
                $result['status']  = -400;
                $result['message'] = $no_desc . '请输入正确的排序';
    
                return $result;
            }
    
            $spec_sattr_value_list = $spec['sattr_value_list'];
            $check_spec_sattr_value_list_result = $this->check_spec_sattr_value_list($spec_sattr_value_list, $sattr_count);
            if($check_spec_sattr_value_list_result['status'] < 0){
                $check_spec_sattr_value_list_result['message'] = $no_desc . $check_spec_sattr_value_list_result['message'];
    
                return $check_spec_sattr_value_list_result;
            }else{
                $spec_sattr_value_list = $check_spec_sattr_value_list_result['data']['spec_sattr_value_list'];
            }
    
            $spec_number_grade_price_list = $spec['number_grade_price_list'];
            $check_spec_number_grade_price_list_result = $this->check_spec_number_grade_price_list($spec_number_grade_price_list, $number_grade_count);
            if($check_spec_number_grade_price_list_result['status'] < 0){
                $check_spec_number_grade_price_list_result['message'] = $no_desc . $check_spec_number_grade_price_list_result['message'];
    
                return $check_spec_number_grade_price_list_result;
            }else{
                $spec_number_grade_price_list = $check_spec_number_grade_price_list_result['data']['spec_number_grade_price_list'];
            }
    
            $spec_list[$key]['order'] = $order;
            $spec_list[$key]['sattr_value_list'] = $spec_sattr_value_list;
            $spec_list[$key]['number_grade_price_list'] = $spec_number_grade_price_list;
    
            $no++;
        }
    
        $result['data']['spec_list'] = $spec_list;
         
        return $result;
    }
    
    public function check_spec_sattr_value_list($spec_sattr_value_list, $sattr_count)
    {
        $result = array('status' => 0, 'message' => '', 'data' => array());
         
        if(empty($spec_sattr_value_list)){
            $result['status']  = -400;
            $result['message'] = '请输入规格属性值';
             
            return $result;
        }
         
        if(!is_array($spec_sattr_value_list)){
            $result['status']  = -400;
            $result['message'] = '规格属性值格式错误';
             
            return $result;
        }
    
        $spec_sattr_value_count = count($spec_sattr_value_list);
        if($spec_sattr_value_count !== $sattr_count){
            $result['status']  = -400;
            $result['message'] = '属性值与属性数量不对应';
             
            return $result;
        }
    
        $no = 1;
        foreach ($spec_sattr_value_list as $key => $spec_sattr_value) {
            $no_message = '请输入第' . $no . '个属性值';
    
            $spec_sattr_value = init_user_var($spec_sattr_value, 'str');
            if($spec_sattr_value === null){
                $result['status']  = -400;
                $result['message'] = $no_message;
    
                return $result;
            }
             
            $spec_sattr_value_list[$key] = $spec_sattr_value;
    
            $no++;
        }
         
        $result['data']['spec_sattr_value_list'] = $spec_sattr_value_list;
    
        return $result;
    }
    
    
    public function check_spec_number_grade_price_list($spec_number_grade_price_list, $number_grade_count)
    {
        $result = array('status' => 0, 'message' => '', 'data' => array());
    
        if(empty($spec_number_grade_price_list)){
            $result['status']  = -400;
            $result['message'] = '请输入数量范围价格列表';
    
            return $result;
        }
    
        if(!is_array($spec_number_grade_price_list)){
            $result['status']  = -400;
            $result['message'] = '数量范围价格列表格式错误';
    
            return $result;
        }
    
        $spec_number_grade_price_count = count($spec_number_grade_price_list);
        if($spec_number_grade_price_count !== $number_grade_count){
            $result['status']  = -400;
            $result['message'] = '数量范围价格列表数量与数量范围列表数量不对应';
    
            return $result;
        }
    
        $no = 1;
        foreach ($spec_number_grade_price_list as $key => $spec_number_grade_price) {
            $no_message = '请输入第' . $no . '个数量范围价格';
    
            $spec_number_grade_price = init_user_var($spec_number_grade_price, 'float');
            if($spec_number_grade_price === null){
                $result['status']  = -400;
                $result['message'] = $no_message;
    
                return $result;
            }
    
            $spec_number_grade_price_list[$key] = $spec_number_grade_price;
    
            $no++;
        }
    
        $result['data']['spec_number_grade_price_list'] = $spec_number_grade_price_list;
    
        return $result;
    }
    
    /****************** @end private function  ******************/
}